<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()

// 表单数据
const formData = ref({
  name: '',
  email: '',
  password: '',
  confirmPassword: '',
  agreeTerms: false
})

// 表单验证
const errors = ref({
  name: '',
  email: '',
  password: '',
  confirmPassword: '',
  agreeTerms: ''
})

// 注册状态
const isRegistering = ref(false)

// 验证表单
const validateForm = () => {
  let isValid = true
  errors.value = {
    name: '',
    email: '',
    password: '',
    confirmPassword: '',
    agreeTerms: ''
  }

  if (!formData.value.name) {
    errors.value.name = '请输入姓名'
    isValid = false
  }

  if (!formData.value.email) {
    errors.value.email = '请输入邮箱'
    isValid = false
  } else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(formData.value.email)) {
    errors.value.email = '请输入有效的邮箱地址'
    isValid = false
  }

  if (!formData.value.password) {
    errors.value.password = '请输入密码'
    isValid = false
  } else if (formData.value.password.length < 6) {
    errors.value.password = '密码长度不能少于 6 位'
    isValid = false
  }

  if (!formData.value.confirmPassword) {
    errors.value.confirmPassword = '请确认密码'
    isValid = false
  } else if (formData.value.password !== formData.value.confirmPassword) {
    errors.value.confirmPassword = '两次输入的密码不一致'
    isValid = false
  }

  if (!formData.value.agreeTerms) {
    errors.value.agreeTerms = '请同意服务条款和隐私政策'
    isValid = false
  }

  return isValid
}

// 提交表单
const submitForm = async () => {
  if (!validateForm()) return

  isRegistering.value = true
  try {
    // 这里添加实际的注册逻辑
    await new Promise(resolve => setTimeout(resolve, 1000))
    router.push('/login')
  } catch (error) {
    console.error('注册失败:', error)
  } finally {
    isRegistering.value = false
  }
}

// 跳转到登录页面
const goToLogin = () => {
  router.push('/login')
}
</script>

<template>
  <div class="register">
    <div class="register-container">
      <div class="register-header">
        <h1>创建账号</h1>
        <p>加入我们，开启 AI 之旅</p>
      </div>

      <form class="register-form" @submit.prevent="submitForm">
        <div class="form-group">
          <label for="name">姓名</label>
          <input
            type="text"
            id="name"
            v-model="formData.name"
            :class="{ 'error': errors.name }"
            placeholder="请输入姓名"
          />
          <span class="error-message" v-if="errors.name">{{ errors.name }}</span>
        </div>

        <div class="form-group">
          <label for="email">邮箱</label>
          <input
            type="email"
            id="email"
            v-model="formData.email"
            :class="{ 'error': errors.email }"
            placeholder="请输入邮箱"
          />
          <span class="error-message" v-if="errors.email">{{ errors.email }}</span>
        </div>

        <div class="form-group">
          <label for="password">密码</label>
          <input
            type="password"
            id="password"
            v-model="formData.password"
            :class="{ 'error': errors.password }"
            placeholder="请输入密码"
          />
          <span class="error-message" v-if="errors.password">{{ errors.password }}</span>
        </div>

        <div class="form-group">
          <label for="confirmPassword">确认密码</label>
          <input
            type="password"
            id="confirmPassword"
            v-model="formData.confirmPassword"
            :class="{ 'error': errors.confirmPassword }"
            placeholder="请再次输入密码"
          />
          <span class="error-message" v-if="errors.confirmPassword">
            {{ errors.confirmPassword }}
          </span>
        </div>

        <div class="form-group">
          <label class="terms-checkbox">
            <input
              type="checkbox"
              v-model="formData.agreeTerms"
              :class="{ 'error': errors.agreeTerms }"
            />
            <span>
              我已阅读并同意
              <a href="#" target="_blank">服务条款</a>
              和
              <a href="#" target="_blank">隐私政策</a>
            </span>
          </label>
          <span class="error-message" v-if="errors.agreeTerms">{{ errors.agreeTerms }}</span>
        </div>

        <button type="submit" class="btn-register" :disabled="isRegistering">
          {{ isRegistering ? '注册中...' : '注册' }}
        </button>

        <div class="register-footer">
          <p>
            已有账号？
            <a href="#" @click.prevent="goToLogin">立即登录</a>
          </p>
        </div>
      </form>

      <div class="register-divider">
        <span>或</span>
      </div>

      <div class="social-register">
        <button class="btn-social btn-google">
          <span class="social-icon">🌐</span>
          使用 Google 账号注册
        </button>
        <button class="btn-social btn-github">
          <span class="social-icon">🐙</span>
          使用 GitHub 账号注册
        </button>
      </div>
    </div>
  </div>
</template>

<style scoped>
.register {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  padding: 2rem;
}

.register-container {
  width: 100%;
  max-width: 400px;
  background: white;
  border-radius: 10px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  padding: 2rem;
}

.register-header {
  text-align: center;
  margin-bottom: 2rem;
}

.register-header h1 {
  font-size: 2rem;
  color: #333;
  margin-bottom: 0.5rem;
}

.register-header p {
  color: #666;
}

.register-form {
  margin-bottom: 2rem;
}

.form-group {
  margin-bottom: 1.5rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  color: #333;
  font-weight: 500;
}

.form-group input {
  width: 100%;
  padding: 0.75rem;
  border: 1px solid #ddd;
  border-radius: 5px;
  font-size: 1rem;
  transition: border-color 0.3s ease;
}

.form-group input:focus {
  border-color: #007bff;
  outline: none;
}

.form-group input.error {
  border-color: #dc3545;
}

.error-message {
  color: #dc3545;
  font-size: 0.875rem;
  margin-top: 0.25rem;
}

.terms-checkbox {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  color: #666;
  cursor: pointer;
}

.terms-checkbox input[type="checkbox"] {
  width: 1rem;
  height: 1rem;
  margin-top: 0.25rem;
}

.terms-checkbox a {
  color: #007bff;
  text-decoration: none;
}

.terms-checkbox a:hover {
  text-decoration: underline;
}

.btn-register {
  width: 100%;
  padding: 0.75rem;
  background: #007bff;
  color: white;
  border: none;
  border-radius: 5px;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.3s ease;
}

.btn-register:hover {
  background: #0056b3;
}

.btn-register:disabled {
  background: #ccc;
  cursor: not-allowed;
}

.register-footer {
  text-align: center;
  margin-top: 1.5rem;
  color: #666;
}

.register-footer a {
  color: #007bff;
  text-decoration: none;
}

.register-footer a:hover {
  text-decoration: underline;
}

.register-divider {
  position: relative;
  text-align: center;
  margin: 2rem 0;
}

.register-divider::before,
.register-divider::after {
  content: '';
  position: absolute;
  top: 50%;
  width: calc(50% - 30px);
  height: 1px;
  background: #ddd;
}

.register-divider::before {
  left: 0;
}

.register-divider::after {
  right: 0;
}

.register-divider span {
  background: white;
  padding: 0 1rem;
  color: #666;
  font-size: 0.875rem;
}

.social-register {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.btn-social {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  width: 100%;
  padding: 0.75rem;
  border: 1px solid #ddd;
  border-radius: 5px;
  background: white;
  color: #333;
  font-size: 1rem;
  cursor: pointer;
  transition: all 0.3s ease;
}

.btn-social:hover {
  background: #f8f9fa;
}

.btn-social img {
  width: 1.5rem;
  height: 1.5rem;
}

.btn-google {
  color: #ea4335;
}

.btn-github {
  color: #333;
}

.social-icon {
  font-size: 1.5rem;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .register-container {
    padding: 1.5rem;
  }

  .register-header h1 {
    font-size: 1.75rem;
  }
}
</style> 